/*********************************************************************************
Name:  updateProjectMap()
Copyright © 2012  Lodha Group
======================================================
======================================================
Purpose:
-------
To update leadsource and type of accomodation mapping for visitor form settings
======================================================
======================================================
History
-------
VERSION    AUTHOR                DATE             DETAIL              
1.0 -   CG Dev Team          15/03/2012      INITIAL DEVELOPMENT  
*********************************************************************************/ 
trigger updateProjectMap on Project_Name__c (before insert,before update) {
    List<Project_Name__c> projectList = trigger.new;

    //get the leadsource picklist values form visitorformsettings object
    List<String> leadSourceList = ProjectMappingController.getPicklistDescribe('VisitorFormSettings__c','LeadSource__c');
    
    // for new projects
    if(trigger.isInsert){
        for(Project_Name__c project:projectList){
            
            //populate accomodationMap
            if(project.Type_of_accomodation__c!=null){
                project.accomodationMap__c = '';
                for(String accomodationValue : project.Type_of_accomodation__c.split(';') ){
                        project.accomodationMap__c += accomodationValue+','+accomodationValue+';';            
                }                            
            }
            
            //populate leadSourceMap
            project.leadSourceMap__c = '';
            for(String leadSource: leadSourceList ){
                    project.leadSourceMap__c += leadSource+','+leadSource+';';            
            }
        }
    }

    // for exisiting projects on update
    if(trigger.isUpdate){
        for(Project_Name__c project:projectList){
            
            //populate accomodationMap 
            if(project.Type_of_accomodation__c!=null){
                
                if(project.accomodationMap__c==null){
                    project.accomodationMap__c = '';
                    for(String accomodationValue : project.Type_of_accomodation__c.split(';') ){
                            project.accomodationMap__c += accomodationValue+','+accomodationValue+';';            
                    }                            
                }
                else{
                    //upadte the accomodationMap
                    Set<String> addedAccomodation =new Set<String>();
                    map<String,String> accomodationMap = new  map<String,String>();
                    String[] accomodationMappedList = project.accomodationMap__c.split(';');
                    project.accomodationMap__c = '';
                    for(String accomodationMapped: accomodationMappedList){
                        String[] accomodationMapValue = accomodationMapped.split(',');
                        accomodationMap.put(accomodationMapValue[0],accomodationMapValue[1]);  
                    }                    
                    for(String accomodationValue : project.Type_of_accomodation__c.split(';')){
                        if(accomodationMap.keyset().contains(accomodationValue)){
                            project.accomodationMap__c += accomodationValue+','+accomodationMap.get(accomodationValue)+';'; 
                        }
                        else{
                            project.accomodationMap__c +=accomodationValue+','+accomodationValue+';';
                        }
                    }
                }
            }
            
            //populate leadSourceMap if empty
            if(project.leadSourceMap__c==null){
                project.leadSourceMap__c = '';
                for(String leadSource: leadSourceList ){
                        project.leadSourceMap__c += leadSource+','+leadSource+';';            
                }                            
            }
            else{
                Set<String> addedLeadSource =new Set<String>();
                map<String,String> leadSourceMap = new  map<String,String>();
                String[] leadSourceMappedList = project.leadSourceMap__c.split(';');
                project.leadSourceMap__c = '';
                for(String leadSourceMapped: leadSourceMappedList){
                    String[] leadSourceMapValue = leadSourceMapped.split(',');
                    leadSourceMap.put(leadSourceMapValue[0],leadSourceMapValue[1]);  
                }                    
                for(String leadSourceValue : leadSourceList ){
                    if(leadSourceMap.keyset().contains(leadSourceValue)){
                        project.leadSourceMap__c += leadSourceValue+','+leadSourceMap.get(leadSourceValue)+';'; 
                    }
                    else{
                        project.leadSourceMap__c +=leadSourceValue+','+leadSourceValue+';';
                    }
                }           
            }
        }
    }   
}